import Axios from 'axios';
import { Loading } from 'element-ui';
import { message } from '@/utils/resetMessage';
import { baseURL } from '@/utils/base.js'

export function exportExcel(data, excelName, apiUrl) {
    // 导出

    let loadingInstance = Loading.service({
        text: '正在处理数据'
    });
    data.pageNum = 1
    data.pageSize = 10000
    console.log(sessionStorage.getItem('ms_token'))
    var api = baseURL + apiUrl;
    Axios.post(api, data,
        {
            headers: {
                'Content-Type': "application/json",
                "x-access-token": sessionStorage.getItem('token'),
            },
            responseType: 'blob'
        }).then(res => {
            var fileName = excelName + ".xlsx";
            var blob = res.data;
            var a = document.createElement('a');
            a.innerHTML = fileName;
            a.download = fileName;
            a.href = window.URL.createObjectURL(blob);
            document.body.appendChild(a);
            var evt = document.createEvent("MouseEvents");
            evt.initEvent("click", false, false);
            a.dispatchEvent(evt);
            document.body.removeChild(a);
            loadingInstance.close()
        }).catch(err => {
            loadingInstance.close()
            console.log(err);
            message.error('导出失败');
        })

}